﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Lexalytics;

namespace SalienceEngineDemo.Dialogs
{
    public partial class ThemeDetails : Form
    {
        private List<SalienceTheme> m_Themes = null;
        private int m_ThemeIndex = 0;
        private string m_Content = String.Empty;
        private DataGridView m_SourceView = null;

        public ThemeDetails(DataGridView dgSource, List<SalienceTheme> lstThemes, string strContent)
        {
            m_ThemeIndex = dgSource.SelectedRows[0].Index;
            m_Themes = lstThemes;
            m_Content = strContent;
            m_SourceView = dgSource;
            InitializeComponent();
        }

        #region Event Handlers
        private void ThemeDetails_Load(object sender, EventArgs e)
        {
            PopulateFormData();
            SetNavButtons();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (m_ThemeIndex > 0)
            {
                m_SourceView.Rows[m_ThemeIndex].Selected = false;
                m_ThemeIndex--;
                m_SourceView.Rows[m_ThemeIndex].Selected = true;
                PopulateFormData();
                SetNavButtons();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (m_ThemeIndex < m_Themes.Count - 1)
            {
                m_SourceView.Rows[m_ThemeIndex].Selected = false;
                m_ThemeIndex++;
                m_SourceView.Rows[m_ThemeIndex].Selected = true;
                PopulateFormData();
                SetNavButtons();
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion // Event Handlers

        private void SetNavButtons()
        {
            btnPrev.Enabled = (m_ThemeIndex > 0);
            btnNext.Enabled = (m_ThemeIndex < m_Themes.Count - 1);
        }

        private void PopulateFormData()
        {
            Lexalytics.SalienceTheme aTheme = m_Themes[m_ThemeIndex];

            // Set dialog title
            this.Text = String.Format("Details for {0}", aTheme.sTheme);
            // Insert general entity details
            lblNormalizedForm.Text = aTheme.sTheme;
            lblSentiment.Text = String.Format("Sentiment score: {0}", aTheme.fSentiment);
            lblEvidence.Text = String.Format("Evidence score: {0}", aTheme.nEvidence);
            txtSummary.Text = aTheme.sSummary;


            // Insert topic list
            int nTopicCount = 0;
            dgTopics.Rows.Clear();
            foreach (SalienceTopic aTopic in aTheme.Topics)
            {
                dgTopics.Rows.Add();
                dgTopics.Rows[nTopicCount].Cells[0].Value = aTopic.sTopic;
                dgTopics.Rows[nTopicCount].Cells[1].Value = aTopic.fSentiment;
                dgTopics.Rows[nTopicCount].Cells[2].Value = aTopic.fScore;
                dgTopics.Rows[nTopicCount].Cells[3].Value = aTopic.nHits;
                nTopicCount++;
            }

            // Insert sentiment phrases
            int nPhraseCount = 0;
            dgPhrases.Rows.Clear();
            foreach (SaliencePhrase aPhrase in aTheme.ThemeMentions)
            {
                dgPhrases.Rows.Add();
                dgPhrases.Rows[nPhraseCount].Cells[0].Value = aPhrase.sText;
                if (aPhrase.nNegated == 1)
                    dgPhrases.Rows[nPhraseCount].Cells[1].Value = "Yes";
                else
                    dgPhrases.Rows[nPhraseCount].Cells[1].Value = "No";
                dgPhrases.Rows[nPhraseCount].Cells[2].Value = aPhrase.sNegator;
                nPhraseCount++;
            }

        }
    }
}
